package com.bilibili.juc.cf;

import java.util.concurrent.*;

/**
 * @auther zzyy
 * @create 2022-01-17 16:36
 */
public class CompletableFutureAPI2_3Demo
{
    public static void main(String[] args) throws Exception
    {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            //暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
            return "1";
        }, threadPool).handle((f, e) -> {
            if (e != null) {
                System.out.println("!222 exception ==>> " + e.getMessage());
                throw new RuntimeException("222 exception occur",e);
            }
            int i = 10 / 0;
            System.out.println("222 pass value ==>> " + f);
            return f + "->" + 2;
        }).handle((f, e) -> {
            if (e != null) {
                System.out.println("@333 exception ==>> " + e.getMessage());
                throw new RuntimeException("333 exception occur",e);
            }
            System.out.println("333 pass value ==>> " + f);
            return f + "->" + 3;
        }).handle((f, e) -> {
            if (e != null) {
                System.out.println("#444 exception ==>> " + e.getMessage());
                throw new RuntimeException("444 exception occur",e);
            }
            System.out.println("444 pass value ==>> " + f);
            return f + "->" + 4;
        }).whenComplete((v, e) -> {
            if (e != null) {
                System.out.println("^^^whenComplete exception ==>> " + e.getMessage());
                throw new RuntimeException("whenComplete exception occur",e);
            }
            if (e == null) {
                System.out.println("----计算结果： " + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println("****** exceptionally 捕获到的异常信息==>> " + e.getMessage());
            return null;
        });

        System.out.println(Thread.currentThread().getName()+"----主线程先去忙其它任务");


        System.in.read();
        System.out.println("program end");
        threadPool.shutdown();
    }
}

/*
main----主线程先去忙其它任务
111
@333 exception ==>> java.lang.ArithmeticException: / by zero
#444 exception ==>> java.lang.RuntimeException: 333 exception occur
^^^whenComplete exception ==>> java.lang.RuntimeException: 444 exception occur
****** exceptionally 捕获到的异常信息==>> java.lang.RuntimeException: 444 exception occur
java.util.concurrent.CompletionException: java.lang.RuntimeException: 444 exception occur
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:936)
	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
	Suppressed: java.lang.RuntimeException: whenComplete exception occur
		at com.bilibili.juc.cf.CompletableFutureAPI2_3Demo.lambda$main$4(CompletableFutureAPI2_3Demo.java:49)
		at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
		at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
		... 5 more
	Caused by: [CIRCULAR REFERENCE: java.util.concurrent.CompletionException: java.lang.RuntimeException: 444 exception occur]
Caused by: java.lang.RuntimeException: 444 exception occur
	at com.bilibili.juc.cf.CompletableFutureAPI2_3Demo.lambda$main$3(CompletableFutureAPI2_3Demo.java:42)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	... 6 more
Caused by: java.util.concurrent.CompletionException: java.lang.RuntimeException: 333 exception occur
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:936)
	... 6 more
Caused by: java.lang.RuntimeException: 333 exception occur
	at com.bilibili.juc.cf.CompletableFutureAPI2_3Demo.lambda$main$2(CompletableFutureAPI2_3Demo.java:35)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	... 6 more
Caused by: java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:936)
	... 6 more
Caused by: java.lang.ArithmeticException: / by zero
	at com.bilibili.juc.cf.CompletableFutureAPI2_3Demo.lambda$main$1(CompletableFutureAPI2_3Demo.java:29)
	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
	... 6 more

program end
 */
